package src.com.doit.demo02;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class test_HsahMap {
    public static void main(String[] args) {
        Map<Student,String> has = new HashMap<>();
        has.put(new Student("柳岩",18),"北京");
        has.put(new Student("柳岩",18),"上海");
        has.put(new Student("赵露思",28),"石家庄");
        has.put(new Student("段红光",29),"小呆没");
        //键值遍历
        Set<Student> students = has.keySet();
        System.out.println("键值for遍历==========");
        for (Student s:students)
        {
            String s1 = has.get(s);
            System.out.println(s.getName()+s.getAge()+" "+s1);
        }
        System.out.println("键值迭代器遍历============");
        Iterator<Student> it = students.iterator();
        while (it.hasNext()){
            Student next = it.next();
            System.out.println(next.getName()+next.getAge()+" "+has.get(next));
        }

        //键值对遍历
        Set<Map.Entry<Student, String>> entries = has.entrySet();
        System.out.println("键值对for遍历================");
        for (Map.Entry<Student, String> s:entries)
        {
            Student key = s.getKey();
            String value = s.getValue();
            System.out.println(key.getName()+key.getAge()+" "+value);
        }
        System.out.println("键值对迭代器遍历=================");
        Iterator<Map.Entry<Student, String>> itt = entries.iterator();
        while(itt.hasNext()){
            Map.Entry<Student, String> next = itt.next();
            Student key = next.getKey();
            String value = next.getValue();
            System.out.println(key.getName()+key.getAge()+" "+value);
        }
    }
}
